Gráficos dinámicos

Elizabeth Valles

Enero, 2022

library(tidyverse)
library(gganimate)
library(gapminder)
library(gifski)
library(plotly)
library(scales)
library(ggpubr)
library(ggstatsplot)
datos.animate <- read_csv("/home/eli/Desktop/Neurobiologia/Platicas/IGeografia/data/datos.code.csv")
glimpse(datos.animate)
## Rows: 928
## Columns: 14
## $ state_name                     <chr> "Aguascalientes", "Baja California", "B…
## $ año                            <dbl> 1992, 1992, 1992, 1992, 1992, 1992, 199…
## $ cuartos                        <dbl> 636855, 3247343, 1793106, 557151, 13858…
## $ state_code                     <dbl> 1, 2, 3, 4, 7, 8, 9, 5, 6, 10, 15, 11, …
## $ nom_abr                        <chr> "Ags.", "BC", "BCS", "Camp.", "Chis.", …
## $ capital_name                   <chr> "Aguascalientes", "Ensenada", "Comondú"…
## $ mpe_code                       <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
## $ municipio_name                 <chr> "Aguascalientes", "Ensenada", "Ciudad C…
## $ latitud                        <chr> "21°52´47.362\"N", "31°48´32.198\"N", "…
## $ longitud                       <chr> "102°17´45.768\"W", "116°35´42.482\"W",…
## $ pob_total                      <dbl> 863893, 330652, 43805, 15949, 8092, 105…
## $ pob_masculina                  <dbl> 419168, 163313, 21645, 7732, 3914, 5207…
## $ pob_femenina                   <dbl> 444725, 167339, 22160, 8217, 4178, 5303…
## $ `total de viviendas habitadas` <dbl> 246259, 105810, 13329, 4100, 2107, 3134…

Gráfico con ggplot

plot.cuartos <- ggplot(data = datos.animate, mapping = aes(state_name, cuartos, 
                                                           color = state_name, 
                                                           size = año)) +
  geom_point(aes(frame = año), alpha = 0.5) +
  scale_fill_hue(l=40, c=60) +
  ggtitle("Cuartos por Estado-Año") +
  theme(axis.text = element_text(size = 8)) +
  coord_flip() +
  scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6))
plot.cuartos

Gráfico con ggplotly

ggplotly(plot.cuartos)

Box plot

box.plot <- datos.animate %>% 
  ggplot(mapping = aes(state_name, cuartos, fill = state_name))  +
  geom_boxplot() +
  coord_flip() +
  theme(axis.text = element_text(size = 8)) +
  scale_fill_hue(l=40, c=60) +
  ggtitle("Número de cuartos ocupados por Estado") +
  guides(fill = FALSE) +
  scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6), n.breaks = 20) +
  stat_summary(fun.y = mean, geom = "point")  
box.plot

Gráfico con ggplotly

ggplotly(box.plot)

Violin plot con ggstatsplot

violin.ggstatsplot <- datos.animate %>%
  filter(state_name %in% c("Colima","Oaxaca","Tamaulipas", "Veracruz")) %>% 
  ggbetweenstats(x = state_name, y = cuartos, 
                 method = "np", 
                 p.adjust.method = "bonferroni", 
                 title = "Cuartos por estado",
                 xlab = "Estado", 
                 ylab = "No. de cuartos",
                 ggplot.component = list(ggplot2::scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6), n.breaks = 10)))
violin.ggstatsplot

Gráfico con ggplotly

ggplotly(violin.ggstatsplot)

Gráfico con ggplot animate

ggplot(data = datos.animate, mapping = aes(state_name, cuartos, 
                                           color = state_name,
                                           size = año)) +
  geom_point(alpha = 0.5) +
  scale_fill_hue(l=40, c=60) +
  ggtitle("Cuartos por Estado-Año") +
  theme(axis.text = element_text(size = 8)) +
  coord_flip() +
  scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6)) +
  transition_time(año) +
  ease_aes('linear')

Bar plot: transition_time

datos.animate %>% 
  ggplot()  +
  geom_col(aes(state_name, cuartos, fill = factor(año))) +
  theme(axis.text = element_text(size = 8)) +
  ggtitle("Número de cuartos ocupados por Estado") +
  coord_flip() +
  transition_time(año) +
  scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6))

Bar plot: title{frame_time}

datos.animate %>% 
  ggplot()  +
  geom_col(aes(state_name, cuartos, fill = factor(año))) +
  theme(axis.text = element_text(size = 8)) +
  ggtitle("Número de cuartos ocupados por Estado") +
  scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6)) +
  coord_flip() +
  transition_time(año) +
  labs(title = "año: {frame_time}")

Line plot: transition_reveal

datos.animate %>% 
  filter(state_name %in% c("Colima","Oaxaca","Tamaulipas", "Veracruz")) %>%
  ggplot(aes(año, cuartos, color = state_name))  +
  geom_point() +
  geom_line() +
  scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6)) +
  theme(axis.text = element_text(size = 8)) +
  ggtitle("Número de cuartos ocupados por año") +
  transition_reveal(año)

Line plot: view_follow

datos.animate %>% 
  filter(state_name %in% c("Colima","Oaxaca","Tamaulipas", "Veracruz")) %>%
  ggplot(aes(año, cuartos, color = state_name))  +
  geom_point() +
  geom_line() +
  scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6)) +
  theme(axis.text = element_text(size = 8)) +
  ggtitle("Número de cuartos ocupados por año") +
  transition_reveal(año) +
  view_follow()

Actividad: aplicando las funciones de plotly o gganimate